package com.jin.lazytool.task;

import com.fasterxml.jackson.annotation.JsonIgnore;
import com.jin.lazytool.entity.IEvent;
import java.awt.*;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Timer;

public class CopyTask implements Serializable {

    private List<IEvent> list = new ArrayList<>();

    @JsonIgnore
    public Robot getRobot() {
        return robot;
    }

    private Robot robot;
    @JsonIgnore
    private Timer timer;
    private String name;
    private long scheduleStartTime;

    public long getLoopAddTime() {
        return loopAddTime;
    }

    @JsonIgnore
    private long loopAddTime = 0;

    public void addLoopTime(long time){
        loopAddTime += time;
    }

    private long collectStartTime;

    private boolean hotKeyStart = false;
    private boolean hotKeyStop = false;

    public List<IEvent> getList(){
        return this.list;
    }
    public void setList(List<IEvent> list){
        this.list = list;
    }


    public long handleEventTime(){
        return System.currentTimeMillis() - collectStartTime;
    }

    public void startTime() {
        collectStartTime = System.currentTimeMillis();
    }

    public void add(IEvent actionEvent) {
        list.add(actionEvent);
    }

    public void add(int index, IEvent actionEvent) {
        list.add(index, actionEvent);
    }

    public void removeHotkey(){
        List<IEvent> remove = new ArrayList<>();
        if(hotKeyStop){
            int size = this.list.size();
            remove.addAll(Arrays.asList(this.list.get(size - 1),this.list.get(size - 2),this.list.get(size - 3),this.list.get(size - 4),
                    this.list.get(size - 5),this.list.get(size - 6)));
        }else{
            remove.add(this.list.get(list.size() - 1));
        }
        if(hotKeyStart){
            remove.addAll(Arrays.asList(this.list.get(0),this.list.get(1),this.list.get(2)));
        }
        this.list.removeAll(remove);
    }

    public void execute() {
        timer = new Timer();
        scheduleStartTime = System.currentTimeMillis();
        try {
            robot = new Robot();
        } catch (AWTException e) {
            e.printStackTrace();
            return;
        }
    }

    public Timer getTimer() {
        return timer;
    }

    public void setTimer(Timer timer) {
        this.timer = timer;
    }

    public long getScheduleStartTime() {
        return scheduleStartTime;
    }

    public void setScheduleStartTime(long scheduleStartTime) {
        this.scheduleStartTime = scheduleStartTime;
    }
    public long getCollectStartTime() {
        return collectStartTime;
    }

    public void setCollectStartTime(long collectStartTime) {
        this.collectStartTime = collectStartTime;
    }

    public boolean isHotKeyStart() {
        return hotKeyStart;
    }

    public void setHotKeyStart(boolean hotKeyStart) {
        this.hotKeyStart = hotKeyStart;
    }

    public boolean isHotKeyStop() {
        return hotKeyStop;
    }

    public void setHotKeyStop(boolean hotKeyStop) {
        this.hotKeyStop = hotKeyStop;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

}
